home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / pcscheme / geneva / sources.exe / SOURCES / ASM / BID.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-01-18  |  9.7 KB  |  373 lines

  1. ;* BID.ASM
  2. ;************************************************************************
  3. ;*                                    *
  4. ;*        PC Scheme/Geneva 4.00 Borland TASM code            *
  5. ;*                                    *
  6. ;* (c) 1985-1988 by Texas Instruments, Inc. See COPYRIGHT.TXT        *
  7. ;* (c) 1992 by L. Bartholdi & M. Vuilleumier, University of Geneva    *
  8. ;*                                    *
  9. ;*----------------------------------------------------------------------*
  10. ;*                                    *
  11. ;*                Bid a task                *
  12. ;*                                    *
  13. ;*----------------------------------------------------------------------*
  14. ;*                                    *
  15. ;* Created by: John Jensen        Date: 1985            *
  16. ;* Revision history:                            *
  17. ;* - 18 Jun 92:    Renaissance (Borland Compilers, ...)            *
  18. ;*                                    *
  19. ;*                    ``In nomine omnipotentii dei''    *
  20. ;************************************************************************
  21. IDEAL
  22. %PAGESIZE    60, 132
  23. MODEL    medium
  24. LOCALS    @@
  25.  
  26.     INCLUDE "scheme.ash"
  27.  
  28. MODIFMEM    = 04ah             ; Modify allocated memory function id
  29. BIDTASK        = 04b00h        ; Load and execute program function id
  30. GETRETCODE    = 04dh            ; Get program's return code
  31. CREATE_FL    = 3ch             ; Create file function
  32. OPEN_FL        = 3dh             ; Open file function
  33. CLOSE_FL    = 3eh             ; Close file function
  34. READ_FL        = 3fh             ; Read file function
  35. WRITE_FL    = 40h             ; Write file function
  36. DELETE_FL    = 41h             ; Delete file function
  37. GET_DRIVE    = 19h             ; Current disk function
  38. SET_DRIVE    = 0eh             ; Select disk function
  39. GET_DIR        = 47h             ; Return text of current directory function
  40. SET_DIR        = 3bh             ; Change the current directory function
  41. NEW_FILE    = 5ah            ; Create unique filename
  42. MAXPATHLEN    = 64+13
  43. WRITEBATCH    = 0fffh
  44.      
  45. DATASEG
  46.     EXTRN    C _psp:word
  47.  
  48. tmpfile    DB    "?:\", MAXPATHLEN dup(?)
  49. comspec    DB    "COMSPEC="
  50. LENCOMSPEC = $-comspec
  51.  
  52. UDATASEG
  53. LABEL    paramblk
  54. envptr    DW    ?
  55. cmdptr    DD    ?
  56. fcb1    DD    ?
  57. fcb2    DD    ?
  58.  
  59. STRUC    XMSBLOCK
  60. len    dd    ?
  61. shandle    dw    ?
  62. soff    dd    ?
  63. dhandle    dw    ?
  64. doff    dd    ?
  65. ENDS    XMSBLOCK
  66.  
  67. CODESEG
  68. ;************************************************************************
  69. ;*                 Bid another Task                *
  70. ;************************************************************************
  71. ;
  72. ;  Paragraph Addresses
  73. ;
  74. ;   lastparagraph  --> +--------------------+  <----
  75. ;               |   /|\            |     :  Freed for bidded task,
  76. ;               |    |            |     :  Saved to disk save file
  77. ;               |    | -- free_req   |     :   start:   lastparagraph - free_req
  78. ;               |    |            |     :   length:  free_req
  79. ;               |   \|/            |     :    (free_req >= lastparagraph - first_dos)
  80. ;               |~~~~~~~~~~~~~~~~~~~~|  <----
  81. ;               |            |     :
  82. ;               |      (heap)        |     :  Allocated to stay resident
  83. ;               |            |     :   # paras:  lastparagraph -
  84. ;  firstparagraph  --> +--------------------+     :         _psp -
  85. ;               |   (unused area)    |     :          free_req
  86. ;     first_dos --> +--------------------+     :
  87. ;               |            |     :
  88. ;               |      (PCS)        |     :
  89. ;               |            |     :
  90. ;               |            |     :
  91. ;         _psp  --> +--------------------+  <----
  92. ;               |            |
  93. ;
  94.      
  95. PROC    delete    near             ; Deletes the save file
  96.     lea    dx, [tmpfile] 
  97.     mov    ah, DELETE_FL 
  98.     int    MSDOS 
  99.     ret    
  100. ENDP    
  101.  
  102. PROC C    bid_task USES si di, @@file, @@param, @@freereq
  103.     LOCAL    @@xmsaddr:DWORD, @@xmsblock:XMSBLOCK
  104.                     ; Check if requested # of free paragraphs within bounds
  105.     cmp    [@@freereq], 0         ; default to free max?
  106.     je    @@freeall
  107.     mov    ax, [paragraphnum]     ; compute requested base of free area
  108.     sub    ax, [@@freereq]     ; request greater than all memory?
  109.     jb    @@freeall
  110.     cmp    ax, [first_dos]        ; below base of free-able area?
  111.     jnb    @@requestok
  112. @@freeall:
  113.     mov    ax, [paragraphnum]     ; compute max # of free-able paras
  114.     sub    ax, [first_dos]
  115.     mov    [@@freereq], ax
  116. @@requestok:                ; Save Scheme's user memory
  117.     mov    ax, 4300h        ; try to use XMS
  118.     int    2fh
  119.     cmp    al, 80h
  120.     jne    @@swap2disk
  121.     mov    ax, 4310h
  122.     int    2fh
  123.     mov    [WORD HIGH @@xmsaddr], es
  124.     mov    [WORD LOW @@xmsaddr], bx
  125.     mov    ah, 09h            ; allocate XMS block
  126.     mov    dx, [@@freereq]
  127.     add    dx, 3fh            ; round paragraphs up to kb above
  128.     mov    cl, 6
  129.     shr    dx, cl
  130.     call    [@@xmsaddr]
  131.     or    ax, ax
  132.     jz    @@swap2disk
  133.     lea    si, [@@xmsblock]
  134.     xor    ax, ax
  135.     mov    [(XMSBLOCK si).shandle], ax ; conventional...
  136.     mov    [(XMSBLOCK si).dhandle], dx ; ...to extended
  137.     mov    [WORD LOW (XMSBLOCK si).soff], ax
  138.     mov    dx, [paragraphnum]
  139.     sub    dx, [@@freereq]
  140.     mov    [WORD HIGH (XMSBLOCK si).soff], dx
  141.     mov    [WORD LOW (XMSBLOCK si).doff], ax
  142.     mov    [WORD HIGH (XMSBLOCK si).doff], ax
  143.     mov    bx, [@@freereq]
  144.     mov    cx, 4
  145. @@loop:
  146.     shl    bx, 1
  147.     rcl    ax, 1
  148.     loop    @@loop
  149.     mov    [WORD LOW (XMSBLOCK si).len], bx
  150.     mov    [WORD HIGH (XMSBLOCK si).len], ax
  151.     mov    ah, 0bh            ; move memory block
  152.     call    [@@xmsaddr]
  153.     jmp    @@closeok
  154.  
  155. @@swap2disk:
  156.     mov    [WORD LOW @@xmsaddr], -1
  157.     mov    ah, GET_DRIVE
  158.     int    MSDOS 
  159.     inc    al             ; adjust so A=1
  160.     mov    dl, al
  161.     add    al, 'A'-1
  162.     mov    [tmpfile], al         ; put the drive letter into tmpfile
  163.     lea    si, [tmpfile+3]        ; point to path proper
  164.     mov    ah, GET_DIR         ; get current path
  165.     int    MSDOS 
  166.     mov    ah, NEW_FILE        ; append a unique file name
  167.     xor    cx, cx
  168.     lea    dx, [tmpfile]
  169.     int    MSDOS
  170.                     ; Now open the save file...
  171.     lea    dx, [tmpfile]
  172.     mov    cx, 20h         ; file attribute
  173.     mov    ah, CREATE_FL 
  174.     int    MSDOS
  175.     jnb    @@createok
  176.     jmp    @@return
  177. @@createok:                ; Now dump memory to the file
  178.     mov    bx, ax             ; load file handle
  179.     mov    di, [@@freereq]
  180.     mov    ax, [paragraphnum]     ; compute base of area to free
  181.     sub    ax, [@@freereq]
  182.     push    ds
  183.     mov    ds, ax             ; init ds:dx to base of area to save
  184.     xor    dx, dx
  185. @@writeloop:
  186.     cmp    di, WRITEBATCH         ; can write all paras in one shot?
  187.     jbe    @@writelast
  188.     sub    di, WRITEBATCH         ; dec paras-to-write count
  189.     mov    cx, WRITEBATCH shl 4
  190.     mov    ah, WRITE_FL 
  191.     int    MSDOS
  192.     jc    @@writeerror
  193.     cmp    ax, cx             ; wrote all bytes?
  194.     je    @@writeok
  195.     mov    ax, 20             ; write count error
  196.     jmp    @@writeerror 
  197. @@writeok:
  198.     mov    ax, ds             ; inc buffer pointer
  199.     add    ax, WRITEBATCH 
  200.     mov    ds, ax 
  201.     jmp    @@writeloop
  202. @@writelast:
  203.     mov    cl, 4             ; shift para count to byte count
  204.     shl    di, cl 
  205.     mov    cx, di             ; put byte count into cx
  206.     mov    ah, WRITE_FL 
  207.     int    MSDOS             ; do it
  208.     jb    @@writeerror         ; branch if error
  209.     cmp    ax, cx             ; wrote all bytes?
  210.     je    @@writedone 
  211.     mov    ax, 20             ; indicate write count error
  212. @@writeerror:
  213.     pop    ds
  214.     push    ax             ; save error code
  215.     mov    ah, CLOSE_FL 
  216.     int    MSDOS 
  217.     call    delete 
  218.     jmp    @@exit
  219. @@writedone:
  220.     pop    ds
  221.     mov    ah, CLOSE_FL 
  222.     int    MSDOS 
  223.     jnb    @@closeok
  224.     jmp    @@return
  225. @@closeok:                       ; Free up Scheme's user memory
  226.     mov    es, [first_dos]     ; point es to base of allocated area
  227.     mov    bx, [paragraphnum]     ; compute # paras to remain allocated
  228.     sub    bx, [first_dos]
  229.     sub    bx, [@@freereq]
  230.     mov    ah, MODIFMEM
  231.     int    MSDOS
  232.     jnc    @@memoryok 
  233.     push    ax            ; save error code
  234.     call    delete
  235.     jmp    @@exit
  236.  
  237. @@memoryok:                ; Set up parameter block
  238.     mov    dx, [emshandle]
  239.     cmp    dx, 0ffffh        ; EMS allocated ?
  240.     je    @@savenoems
  241.     mov    ah, 47h            ; save the mapping in case
  242.     int    EMMINT            ; the callee clobbers it (Brief ...)
  243. @@savenoems:
  244.     mov    ax, [@@param]         ; Set up dword pointer to command line
  245.     mov    [WORD LOW cmdptr], ax 
  246.     mov    [WORD HIGH cmdptr], ds 
  247.     mov    es, [_psp]
  248.     mov    ax, [es:02ch]         ; copy current environment ptr to parameter area
  249.     mov    [envptr], ax         
  250.  
  251.     call    unfixint C        ; reset shift-break vector
  252.     call    is_graph_mode C
  253.     or    ax, ax
  254.     jnz    @@shownocursor
  255.     call    zcuron C         ; turn the cursor back on
  256. @@shownocursor:
  257.     push    ds
  258.     pop    es
  259.     mov    dx, [@@file]        ; ds:dx is ptr to program
  260.     lea    bx, [paramblk]
  261.     mov    ax, BIDTASK         ; load "load and execute" ftn id
  262.     int    MSDOS
  263.     jc    @@error
  264.     mov    ah, GETRETCODE
  265.     int    MSDOS
  266.     neg    ax            ; return negative values for OK
  267. @@error:
  268.     push    ax
  269.  
  270.     mov    dx, [emshandle]
  271.     cmp    dx, 0ffffh        ; EMS allocated ?
  272.     je    @@restorenoems
  273.     mov    ah, 48h            ; restore the mapping
  274.     int    EMMINT
  275. @@restorenoems:
  276.     call    is_graph_mode C
  277.     or    ax, ax
  278.     jnz    @@hidenocursor
  279.     call    zcuroff C        ; turn the cursor back off
  280. @@hidenocursor:
  281.     call    fix_intr C        ; set shift-break vector
  282.  
  283.     mov    es, [first_dos]     ; point es to base of allocated area
  284.     mov    bx, [paragraphnum]     ; compute # of all available paras
  285.     sub    bx, [first_dos]
  286.     mov    ah, MODIFMEM
  287.     int    MSDOS
  288.     jnc    @@restoremem 
  289. @@fatal:
  290.     pop    ax             ; throw away bid error code
  291.     call    delete             ; delete save file
  292. @@xmsfatal:
  293.     mov    ax, 8000h        ; indicate cannot continue, 8000h
  294.     jmp    @@return 
  295.      
  296. @@restoremem:                ; Restore Scheme's user memory
  297.     cmp    [WORD LOW @@xmsaddr], -1
  298.     je    @@restoreswap
  299.     
  300.     lea    si, [@@xmsblock]    ; swap source & dest
  301.     mov    ax, [(XMSBLOCK si).dhandle]
  302.     xchg    [(XMSBLOCK si).shandle], ax
  303.     mov    [(XMSBLOCK si).dhandle], ax
  304.     mov    ax, [WORD HIGH (XMSBLOCK si).soff]
  305.     xchg    [WORD HIGH (XMSBLOCK si).doff], ax
  306.     mov    [WORD HIGH (XMSBLOCK si).soff], ax
  307.     mov    ah, 0bh            ; move block
  308.     call    [@@xmsaddr]
  309.     or    ax, ax
  310.     jz    @@xmsfatal
  311.     lea    si, [@@xmsblock]
  312.     mov    dx, [(XMSBLOCK si).shandle]
  313.     mov    ah, 0ah
  314.     call    [@@xmsaddr]
  315.     or    ax, ax
  316.     jz    @@xmsfatal
  317.     jmp    @@exit
  318.  
  319. @@restoreswap:
  320.     lea    dx, [tmpfile]         ; point ds:dx to ASCIZ save file path
  321.     mov    al, 00             ; access code for reading
  322.     mov    ah, OPEN_FL 
  323.     int    MSDOS
  324.     jc    @@fatal            ; abort if cannot open save file
  325.                     ; Now read memory from the file
  326.     mov    bx, ax             ; load file handle
  327.     mov    di, [@@freereq]
  328.     mov    ax, [paragraphnum]     ; compute base of area to restore from disk
  329.     sub    ax, [@@freereq]
  330.     push    ds
  331.     mov    ds, ax             ; init ds:dx to base of area to restore
  332.     xor    dx, dx 
  333. @@readloop:
  334.     cmp    di, WRITEBATCH         ; can read all paras in one shot?
  335.     jbe    @@readlast
  336.     sub    di, WRITEBATCH
  337.     mov    cx, WRITEBATCH shl 4
  338.     mov    ah, READ_FL 
  339.     int    MSDOS
  340.     jc    @@readerror
  341.     cmp    ax, cx             ; read all bytes?
  342.     jne    @@readerror
  343.     mov    ax, ds             ; inc buffer pointer
  344.     add    ax, WRITEBATCH 
  345.     mov    ds, ax 
  346.     jmp    @@readloop
  347. @@readlast:
  348.     mov    cl, 4             ; shift para count to byte count
  349.     shl    di, cl 
  350.     mov    cx, di             ; put byte count into cx
  351.     mov    ah, READ_FL 
  352.     int    MSDOS
  353.     jc    @@readerror
  354.     cmp    ax, cx             ; read all bytes?
  355.     je    @@readdone
  356. @@readerror:
  357.     pop    ds
  358.     mov    ah, CLOSE_FL 
  359.     int    MSDOS 
  360.     jmp    @@fatal
  361. @@readdone:
  362.     pop    ds
  363.     mov    ah, CLOSE_FL 
  364.     int    MSDOS 
  365.     call    delete
  366. @@exit:
  367.     pop    ax
  368. @@return:
  369.     ret
  370. ENDP    bid_task
  371.      
  372.     END    
  373.